package com.test.qnews;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.test.qnews.activity.BaseActivity;
import com.test.qnews.activity.ConstellationActivity;
import com.test.qnews.activity.LoginActivity;
import com.test.qnews.activity.LoveActivity;
import com.test.qnews.activity.QuestionActivity;
import com.test.qnews.activity.SetActivity;
import com.test.qnews.activity.StarActivity;
import com.test.qnews.activity.WeatherActivity;
import com.test.qnews.adapter.DrawerAdapter;
import com.test.qnews.adapter.MainAdapter;
import com.test.qnews.clicklistener.DrawerClickListener;
import com.test.qnews.custom.DividerItemDecoration;
import com.test.qnews.fragment.BeautyFragmentSm;
import com.test.qnews.fragment.EntertainmentFragment;
import com.test.qnews.fragment.HotsportFragment;
import com.test.qnews.fragment.JokeFragment;
import com.test.qnews.fragment.MilitaryFragment;
import com.test.qnews.fragment.RecommendFragment;
import com.test.qnews.fragment.SocietyFragment;
import com.test.qnews.fragment.VideoFragment;
import java.util.ArrayList;

public class MainActivity extends BaseActivity implements View.OnClickListener, DrawerClickListener {
    private TabLayout tabLayout;
    private ArrayList<Fragment> datas;
    private MainAdapter mainAdapter;

    private DrawerAdapter drawerAdapter;
    private RecyclerView recyclerView;

    private DrawerLayout drawerLayout;

    private ViewPager viewPager;

    public static MainActivity mainActivity;
    private LinearLayout star, constellation, weather;


    // 定义一个变量，来标识是否退出
    private static boolean isExit = false;

    Handler mHandler = new Handler() {
        //改变状态
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };


    @Override
    protected int setLayout() {

        mainActivity = this;
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        tabLayout = (TabLayout) findViewById(R.id.tablayout);
        datas = new ArrayList<>();
        datas.add(new RecommendFragment());
        datas.add(new MilitaryFragment());
        datas.add(new HotsportFragment());
        datas.add(new JokeFragment());
        datas.add(new BeautyFragmentSm());
        datas.add(new EntertainmentFragment());
        datas.add(new SocietyFragment());
        datas.add(new VideoFragment());

        mainAdapter = new MainAdapter(getSupportFragmentManager(), datas, this);
        viewPager.setAdapter(mainAdapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabTextColors(Color.BLACK, Color.RED);//tablayout字体变色
        tabLayout.setSelectedTabIndicatorColor(Color.RED);

        recyclerView = (RecyclerView) findViewById(R.id.rv_drawer);
        drawerAdapter = new DrawerAdapter(this);
        recyclerView.setAdapter(drawerAdapter);
        drawerAdapter.setDrawerClickListener(this);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));

        findViewById(R.id.iv_drawer).setOnClickListener(this);
        findViewById(R.id.iv_login).setOnClickListener(this);
        findViewById(R.id.iv_card).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_drawer:
                drawerLayout.openDrawer(Gravity.LEFT);
                break;
            case R.id.iv_login:
                Intent intent = new Intent(this, LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.iv_card:
//                Intent intentCard = new Intent(this, CardActivity.class);
//                startActivity(intentCard);
                showSendDialog();

                break;

        }
    }

    @Override
    public void drawerOnClick(int postion) {
        //SetActivity
        if (postion == 1){
            Intent intent1 = new Intent(this, LoveActivity.class);
            startActivity(intent1);
        }
        if (postion == 2) {
            Intent intentSet = new Intent(this, SetActivity.class);
            startActivity(intentSet);}
            if (postion == 3){
                Intent intent = new Intent(this, QuestionActivity.class);
                startActivity(intent);
            }

    }


    /*
    当按下BACK键时，会被onKeyDown捕获，判断是BACK键，则执行exit方法。
    在exit方法中，会首先判断isExit的值，如果为false的话，则置为true，
    同时会弹出提示，并在2000毫秒（2秒）后发出一个消息，在Handler中将此值还原成false。
    如果在发送消息间隔的2秒内，再次按了BACK键，则再次执行exit方法，
    此时isExit的值已为true，则会执行退出的方法。
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(getApplicationContext(), "再按一次退出程序",
                    Toast.LENGTH_SHORT).show();
            // 利用handler延迟发送更改状态信息
            mHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            finish();
            System.exit(0);
//            Intent intent = new Intent(Intent.ACTION_MAIN);
//            intent.addCategory(Intent.CATEGORY_HOME);
//            startActivity(intent);
//            System.exit(0);
        }
    }

    //显示一个自定义对话框
    private void showSendDialog() {
        //春关键biuld对象,用来设置Dialog
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = LayoutInflater.from(this).inflate(R.layout.activity_card, null);
        star = (LinearLayout) view.findViewById(R.id.layout_star);
        constellation = (LinearLayout) view.findViewById(R.id.layout_constellation);
        weather = (LinearLayout) view.findViewById(R.id.layout_weather);
        weather.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.layout_weather:
                        Intent intent = new Intent(MainActivity.this, WeatherActivity.class);
                        startActivity(intent);

                        break;
                }
            }
        });
        constellation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.layout_constellation:
                        Intent intent = new Intent(MainActivity.this, ConstellationActivity.class);
                        startActivity(intent);

                        break;
                }

            }
        });
        star.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.layout_star:
                        Intent intent = new Intent(MainActivity.this, StarActivity.class);
                        startActivity(intent);

                        break;
                }
            }
        });
        builder.setCancelable(true);
        builder.setView(view);
        builder.show();


    }

}
